package com.cares.core.utils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**  
*   
* @author lee  
* Dom4j 生成XML文档与解析XML文档  
*/ 
@SuppressWarnings("unchecked")
public class Dom4jUtils  {   


	public static String getXmlContent(Document dc){
		Element ele = dc.getRootElement();
		return ele.getText();
	}
	/**
	 * 
	 * @param fileName
	 * @return  根据文件名称得到 document对象
	 * @throws Exception
	 */
	public static Document getXmlDocument(String fileName)throws Exception{
		try{
			SAXReader saxReader = new SAXReader(); 
			return  saxReader.read(ClassUtils.getDefaultClassLoader().getResourceAsStream(fileName));  
		}catch(DocumentException documentException){
			throw documentException;
		}catch(NullPointerException nullPointerException){
			throw nullPointerException;
		}	
		  
	}

	/**
	 * 
	 * @param nodeName
	 * @param document
	 * @return 根据 Document以及节点名称 得到 element数组对象
	 */
	
	public static List<Element> getElementListByNodeName(String nodeName,Document document){
		List<Element> result=new ArrayList<Element>();
		for(Iterator i = document.getRootElement().elementIterator(); i.hasNext();){   
			Element ele = (Element) i.next(); 
			if(nodeName.equals(ele.getName())){
				result.add(ele);
			}
		}
		return result;
	}
	 /**
	  * 
	  * @param document
	  * @param nodeName
	  * @return  根据Document 已经节点名称 得到该节点的内容
	  * @throws Exception
	  */
	public static String getSingleXmlNode(Document document,String nodeName)throws Exception{
		return getSingleXmlElement(document, nodeName).getText();
	}
	/**
	 * 
	 * @param fileName
	 * @param nodeName
	 * @return 根据 文件名称以及节点名字 得到单个节点的内容
	 * @throws Exception
	 */
	public static String getSingleXmlNode(String fileName,String nodeName)throws Exception{
		Document document=getXmlDocument(fileName);
		return getSingleXmlNode(document, nodeName);
	}
   /**
    * 
    * @param nodeName
    * @param element
    * @return 根据元素，已经节点名称 找到 单个Element对象
    */
	public static Element getElementByNodeName(String nodeName,Element element){
		for(Iterator i = element.elementIterator(); i.hasNext();){   
			Element ele = (Element) i.next(); 
			if(nodeName.equals(ele.getName())){
				return ele;
			}
		}
		return null;
	}
	

	

    /**
     * 
     * @param fileName
     * @param nodeName
     * @return 根据文件名称以及节点名称查找元素
     * @throws Exception
     */
	public static Element getSingleXmlElement(String fileName,String nodeName)throws Exception{
		Document document=getXmlDocument(fileName);
		return getSingleXmlElement(document, nodeName);
	}
	 /**
     * 
     * @param fileName
     * @param nodeName
     * @return 根据Document以及节点名称查找元素
     * @throws Exception
     */
	public static Element getSingleXmlElement(Document document,String nodeName)throws Exception{
		if(StringUtils.hasText(nodeName)){
			String[] nodes=nodeName.split("/");
			Element element=document.getRootElement();  
			for(String tempStr:nodes){
				element=getElementByNodeName(tempStr, element);
				if(element==null)break;
			}
			return element;
		}else  throw new NullPointerException();
	}
	public static Boolean hasNode(Document document,String nodeName)throws Exception{
		if(StringUtils.hasText(nodeName)){
			String[] nodes=nodeName.split("/");
			Element element=document.getRootElement();  
			for(String tempStr:nodes){
				element=getElementByNodeName(tempStr, element);
				if(element==null) return false;
			}
			return true;
		}else  throw new NullPointerException();
	}
	
	public static List<String> getXmlNodeList(Document document,String nodeName,String listName)throws Exception{
		List<String> result=new ArrayList<String>();
		// 枚举名称为listName的节点
		List<Element> elementList=getXmlNode(document, nodeName, listName);
		for(Element ele:elementList){
			result.add(ele.getText());
		}
	    return result;
	}
	
	public static List<Element> getXmlNode(Document document,String nodeName,String listName)throws Exception{
		List<Element>result=new ArrayList<Element>();
		Element element = getSingleXmlElement(document, nodeName);
	    for ( Iterator i = element.elementIterator(listName); i.hasNext();) {
	    	 Element foo = (Element) i.next();
	    	 result.add(foo);
	    }
	    return result;
	}
	
	public static Map<String,String> getXmlNodeListWithAttr(Document document,String nodeName,String listName,String attribuateName)throws Exception{
		List<Element>result=getXmlNode(document, nodeName, listName);
		Map<String,String>map =new HashMap<String, String>();
		for(Element ele:result){
			map.put(ele.attributeValue(attribuateName), ele.getText());
		}
	    return map;
	}
	
	public static Map<String,String> getXmlNodeListWithAttr(String fileName,String nodeName,String listName,String attribuateName)throws Exception{
		return getXmlNodeListWithAttr(getXmlDocument(fileName), nodeName, listName, attribuateName);
	}
	/**
	 * @author lee  得到元素属性
	 * @param  name :节点名称 exp:attr
	 * @param  name :元素 exp:<tt attr="test"/>
	 * @return exp: test 如果没有 返回 null
	 */
	public static String getAttr(String name,Element ele){
		if(ele.attribute(name)!=null)return ele.attribute(name).getValue();
		else return null;
		
	}
	/**
	 * 
	 * @param fileName
	 * @param nodeName
	 * @return 根据文件名称以及节点名称 找到所有该节点下的元素内容数组
	 * @throws Exception
	 */
	public static List<String> getChildElementStrList(String fileName,String nodeName)throws Exception{
		List<String> result=new ArrayList<String>();
		 Element temp = getSingleXmlElement(fileName,nodeName);
		 for ( Iterator i = temp.elementIterator(); i.hasNext();) {
	    	  Element foo = (Element) i.next();
	    	  result.add(foo.getText());
	    }
		 return result;
	}
	public static void main(String[] args){
		try {
	
			System.out.println(getChildElementStrList("KamNo.xml", "kamno-runtime"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}   